package com.feixiu.controller;

import com.feixiu.service.StaffService;
import javafx.util.converter.LocalDateTimeStringConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.util.resources.LocaleData;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;


@Controller
@RequestMapping("/certificate")
public class Authory {

    @Autowired
    private StaffService staffService;
    @RequestMapping("/IsLogin")
    public void IsLogin(HttpServletRequest request, HttpServletResponse response, HttpSession session){
       String account= (String) session.getAttribute("account");
       if (account==null){
           try {
               PrintWriter pw= response.getWriter();
               pw.print("false");
           } catch (IOException e) {
               e.printStackTrace();
           }
       }else {
           try {
               PrintWriter pw= response.getWriter();
               pw.print(account);
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
    }
    @RequestMapping(value = "/tologin",method = RequestMethod.POST)
    @ResponseBody
    public String login(HttpServletResponse response,String account,String password, HttpSession session){
        boolean success=staffService.login(account,password);
        if(success) {
            session.setAttribute("account",account);
            return "true";
        }
        return "false";

    }
    @RequestMapping(value = "/getLeft")
    public String getLeft(String account){
        System.out.println("getLeft执行了");
        String s="Left_Approval";
            boolean flag=staffService.isStuff(account);
            if (flag) {
                s= "Left_Stuff";
            }else {
                s="Left_Stuff";
            }

            return s;

    }
    @RequestMapping(value = "/exit")
    @ResponseBody
    public String exit(String account,HttpSession session){

        String s="false";
        session.setAttribute("account",null);
        s="true";
        return s;

    }
}
